package com.pinc.poop;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.AdView;

public class main extends PwdWidgetActivity implements AdListener{
	private SharedPreferences settings;
	private AdView adview;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initWidget();
		Intent i = new Intent(this, BroadcastService.class);
		startService(i);
	}

	@Override
	protected void initWidget() {

		TextView tv_left = (TextView) this.findViewById(R.id.button_left);
		tv_left.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		TextView tv_right = (TextView) this.findViewById(R.id.button_right);
		tv_right.setVisibility(View.GONE);

		ImageView img_set = (ImageView) this.findViewById(R.id.imageView_save);
		img_set.setImageDrawable(getResources().getDrawable(R.drawable.setting));
		img_set.setVisibility(View.VISIBLE);
		img_set.setOnClickListener(new generalClickListener(setting.class));

		TextView tv_title = (TextView) this.findViewById(R.id.textView_title);
		tv_title.setText(getResources().getString(R.string.app_name));

		image_button b_cal = (image_button) this.findViewById(R.id.button_calendar);
		image_button b_st = (image_button) this.findViewById(R.id.button_statis);
		image_button b_abt = (image_button) this.findViewById(R.id.button_about);

		b_cal.setContent(R.drawable.calendar, getResources().getString(R.string.str_calendar)).setTextWidth(120);
		b_cal.setOnClickListener(new generalClickListener(calendar.class));
		b_st.setContent(R.drawable.statistics, getResources().getString(R.string.str_statistics)).setTextWidth(120);
		b_st.setOnClickListener(new generalClickListener(statistic.class));
		b_abt.setContent(R.drawable.about, getResources().getString(R.string.str_about)).setTextWidth(120);
		b_abt.setOnClickListener(new generalClickListener(about.class));

		adview = (AdView) this.findViewById(R.id.adView);
		adview.setAdListener(this);

	}

	@Override
	protected void onResume() {
		super.onResume();

		/** play ad **/
		adview.setVisibility(View.GONE);
		AdRequest request = new AdRequest();
		settings = PreferenceManager.getDefaultSharedPreferences(this);
		String user_sex_str = settings.getString("user_sex", "null");
		if ( user_sex_str.compareTo(getResources().getStringArray(R.array.str_arr_gender)[0]) == 0)
		{
			request.setGender(AdRequest.Gender.FEMALE);
		}
		else if( user_sex_str.compareTo(getResources().getStringArray(R.array.str_arr_gender)[1]) == 0)
		{
			request.setGender(AdRequest.Gender.MALE);
		}
		adview.loadAd(request);
		/** end play ad **/

		new Thread(new Runnable(){
			@Override
			public void run() {
				Parser_Version pv = new Parser_Version();
				if (pv.parse())
				{
					try {
						if ( getPackageManager().getPackageInfo(getPackageName(), 0).versionCode < pv.getVersion())
							main.this.runOnUiThread(new Runnable(){
								@Override
								public void run() {
									Toast.makeText(getApplicationContext(), getResources().getString(R.string.str_update_notify), Toast.LENGTH_LONG).show();
								}
							});
					} catch (NameNotFoundException e) {
						e.printStackTrace();
					}
				}
			}

		}).start();
	}

	private class generalClickListener implements View.OnClickListener
	{
		@SuppressWarnings("rawtypes")
		private Class which_class;

		@SuppressWarnings("rawtypes")
		public generalClickListener(Class c) {
			this.which_class = c;
		}

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(main.this, which_class);
			startActivity(intent);
		}	
	}

	@Override
	public void onDismissScreen(Ad arg0) {
	}

	@Override
	public void onLeaveApplication(Ad arg0) {
	}

	@Override
	public void onPresentScreen(Ad arg0) {
	}

	@Override
	public void onReceiveAd(Ad arg0) {
		adview.setVisibility(View.VISIBLE);
	}

	@Override
	public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
	}

}